SpringBoot:1.5.9.RELEASE
JDK:1.8
IDEA:2017.2
@Value 注解读取 application.properties 文件中的属性
Controller:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExConfigController {
@Value("${my.user.name}")
private String name;
@GetMapping("/user/name")
public String getUserName() {
System.out.println(name);
return name;
}
}
application.properties:1
my.user.name=Hello-孙悟空
中文乱码问题
该方式有时会遇到中文乱码问题,此时可能需要检查一下application.properties文件的编码格式。
以 IDEA 为例:
File -> Settings -> File Encoding
将Properties Files (*.properties)下的Default encoding for properties files设置为UTF-8,将Transparent native-to-ascii conversion前的勾选上。
yml 默认就是 UTF-8 格式,所以天然支持中文。推荐使用 application.yml 替代 application.properties
@ConfigurationProperties 注解及配置类读取 application.properties 或 application.yml 文件中的属性
application.yml:1
2
3my.book.author: 罗贯中
my.book.name: 三国演义
my.book.price: 60
ExConfig :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37package com.example.ec.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
"my.book") (prefix =
public class ExConfig {
private String name;
private String author;
private int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
ExConfigController:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package com.example.ec.controller;
import com.example.ec.config.ExConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExConfigController {
@Autowired
private ExConfig exConfig;
@GetMapping("/book")
public String getBook() {
return exConfig.getAuthor() + " " + exConfig.getName() + " " + exConfig.getPrice();
}
}
不同的 SpringBoot 版本使用方式又些许不同。一些低版本的需要在启动类中配置 @EnableConfigurationProperties 注解
@ConfigurationProperties 注解及配置类读取 custom.properties 或 custom.yml 文件中的属性
读取自定义属性文件中的属性
book.yml:1
2
3my.book.author: 罗贯中
my.book.name: 三国演义
my.book.price: 60
ExConfig :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39package com.example.ec.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
"my.book") (prefix =
"book.yml", encoding = "UTF-8") (value =
public class ExConfig {
private String name;
private String author;
private int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
具体代码已上传GitHub-lxmuse